﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DatabaseSchemaReader;
using DatabaseSchemaReader.DataSchema;

using Happy.Infrastructure.ExtentionMethods;
using Happy.Edge.DatabaseSchemaReader.Extentions;

namespace Happy.Edge.DatabaseSchemaReader
{
    public sealed class SchemaReader
    {
        public async Task<object> ReadAll(object options)
        {
            Check.MustNotNull(options, "options");

            dynamic dynamicOptions = options;

            var dbReader = new DatabaseReader(
                dynamicOptions["connectionString"],
                dynamicOptions["providerName"]
            );

            var _schema = dbReader.ReadAll();

            var tables = _schema
                .Tables
                .Select(table => table.ToDynamic())
                .ToList();

            var views = _schema
                .Views
                .Select(table => table.ToDynamic())
                .ToList();

            return new { 
                tables = tables, 
                views = views 
            };
        }
    }
}
